package com.wu.creditmanager;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wu.creditmanager.entity.Credit;
import com.wu.creditmanager.util.ImageUtil;

public class InfoDialog {

	private Credit credit;
	private AlertDialog dialog;
	private View view;
	private Context context;

	protected InfoDialog(Context context, Credit credit) {
		this.context = context;
		this.credit = credit;
		LayoutInflater mInflater = LayoutInflater.from(context);
		view = mInflater.inflate(R.layout.credit_info_dialog, null);
		dialog = new AlertDialog.Builder(context).setTitle(credit.getName())
				.setIcon(credit.getBank().getIconResId()).setView(view)
				.create();
	}

	public Credit getCredit() {
		return credit;
	}

	public void setCredit(Credit credit) {
		this.credit = credit;
	}

	private void updateInfo() {
		dialog.setTitle(this.credit.getName());
		dialog.setIcon(credit.getBank().getIconResId());
		ImageUtil.setImage(credit.getIcon(),
				(ImageView) view.findViewById(R.id.l_info_id_image));
		((TextView) view.findViewById(R.id.l_info_id_cname)).setText(credit
				.getName());
		((TextView) view.findViewById(R.id.l_info_id_cbank)).setText(credit
				.getBank().getName());
		((TextView) view.findViewById(R.id.l_info_id_clast4code))
				.setText(credit.getLast4code());
		((TextView) view.findViewById(R.id.l_info_id_cstatementday))
				.setText(credit.getStatementDay() + "");
		((TextView) view.findViewById(R.id.l_info_id_rmblimit)).setText(credit
				.getLimitRMB() + "");
		((TextView) view.findViewById(R.id.l_info_id_usdlimit)).setText(credit
				.getLimitUSD() + "");
		((TextView) view.findViewById(R.id.l_info_id_paymentday))
				.setText(credit.getPaymentText(context));
		((TextView) view.findViewById(R.id.l_info_id_recentlypaymentday))
				.setText(new SimpleDateFormat("M-d").format(new Date(credit
						.getNextAlarmTime().getTimeInMillis())));
	}

	public void show() {
		updateInfo();
		dialog.show();
	}
}
